#include "Mario.h"
#include "QuestionBox.h"
#include "Fungi.h"
#include "BreakBrick.h"
#include "Turtle.h"
#include "MushRoom.h"
#include "Coin.h"
#include "Bullet.h"
#include "Pipe.h"

Mario::Mario(D3DXVECTOR2 pos, EMarioState stateMario, EMarioState previousState):GameObject(pos, EObjectType::EMario)
{
	marioState = stateMario;
	this->previousState = previousState;
	smallMario = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::Mario_Small_ID),6,1,6,0,5);
	smallMario->m_TimeAni = 0.1f;
	bigMario = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::Mario_Big_ID),5,1,5,0,4);
	bigMario->m_TimeAni = 0.1f;
	cowboyMario = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::Mario_Gun_ID),5,1,5,0,4);
	cowboyMario->m_TimeAni = 0.1f;
	if(marioState!=Esuperstar)
	{
		switch(marioState)
		{
		case Esmall:
			m_pSprite = smallMario;
			break;
		case Ebig:
			m_pSprite = bigMario;
			break;
		case Egun:
			m_pSprite = cowboyMario;
			break;
		}
	}
	else
	{
		switch(previousState)
		{
		case Esmall:
			m_pSprite = smallMario;
			break;
		case Ebig:
			m_pSprite = bigMario;
			break;
		case Egun:
			m_pSprite = cowboyMario;
			break;
		}
	}
	isLeft = false;
	m_effect = SpriteEffect::None;
	m_velocity = D3DXVECTOR2(0,0);
	hasJumped = true;
	isFall = false;
	isStandInBar =  false;
	canShoot = true;
	isImmortal = false;
	tmpCheck = false;
	tmpCheck2 = false;
	timeShoot = 0;
	timeNextShoot = 0;
	timeChangeState = 0;
	timeCheckLastPoint = 0;
	timeImmortal = 0;
	timeSuperStar = 0;
	positionLastCheck = pos;
	
	isInPiPe = false;

	alpha_immortal = 255;
	alpha_superstar = 255;
	gold = 0;
	score = 0;
	life = 3;
	isLevelFinish = false;
}

Mario::~Mario()
{
	GameObject::~GameObject();
}

void Mario::TurnLeft()
{
	if(marioAction!=ESitting)
	{
		marioAction = ERunning;
		isLeft = true;
		m_effect = SpriteEffect::Horizontally;	
		if(hasJumped==false && marioAction!= EFalling)
		{
			if(marioState==Esmall || (previousState==Esmall&&(marioState==Esuperstar || marioState==Eimmortal)))
			{
				m_pSprite->m_Start = 0;
				m_pSprite->m_End = 1;
			}
			else if(marioState==Ebig || marioState==Egun||((previousState==Ebig || previousState ==Egun)&&(marioState==Esuperstar || marioState==Eimmortal)))
			{
				m_pSprite->m_Start = 0;
				m_pSprite->m_End = 2;
			}
			if(marioState!=Esuperstar)
			{
				if(m_velocity.x > -MARIO_SPEED)
					m_velocity.x -= 0.4f;
				else
					m_velocity.x = -MARIO_SPEED;
			}
			else
			{
				if(m_velocity.x > -SUPER_SPEED)
					m_velocity.x -= 0.6f;
				else
					m_velocity.x = -SUPER_SPEED;
			}
			
		}
		else
		{
			if(m_velocity.x > -MARIO_SPEEDWJUMP)
				m_velocity.x -= 0.2f;
			else
				m_velocity.x = -MARIO_SPEEDWJUMP;
		}
	}
}

void Mario::TurnRight()
{
	if(marioAction != ESitting)
	{
		marioAction = ERunning;
		isLeft = false;
		m_effect = SpriteEffect::None;
		if(hasJumped==false && isFall==false)
		{
			if(marioState==Esmall || (previousState==Esmall&&(marioState==Esuperstar || marioState==Eimmortal)))
			{
				m_pSprite->m_Start = 0;
				m_pSprite->m_End = 1;
			}
			else if(marioState==Ebig || marioState==Egun||((previousState==Ebig || previousState ==Egun)&&(marioState==Esuperstar || marioState==Eimmortal)))
			{
				m_pSprite->m_Start = 0;
				m_pSprite->m_End = 2;
			}
			if(marioState!=Esuperstar)
			{
				if(m_velocity.x < MARIO_SPEED)
					m_velocity.x +=0.4f;
				else
					m_velocity.x = MARIO_SPEED;
			}
			else
			{
				if(m_velocity.x < SUPER_SPEED)
					m_velocity.x +=0.6f;
				else
					m_velocity.x = SUPER_SPEED;
			}
		}
		else
		{
		
			if(m_velocity.x < MARIO_SPEEDWJUMP)
				m_velocity.x +=0.2f;
			else
				m_velocity.x = MARIO_SPEEDWJUMP;
		}
	}
}

void Mario::Jump(int v)
{

	if(marioAction!=ESitting)
	{
		marioAction = EJumping;
		if(hasJumped==false)
		{
			if(marioState ==Esmall || (previousState==Esmall&&(marioState==Esuperstar || marioState==Eimmortal)))
			{
				m_pSprite->m_Start = 5;
				m_pSprite->SetIndex(5);
				m_pSprite->m_End = 5;
			}
			else if(marioState == Ebig || marioState==Egun ||((previousState==Ebig || previousState ==Egun)&&(marioState==Esuperstar || marioState==Eimmortal)))
			{
				m_pSprite->m_Start = 3;
				m_pSprite->SetIndex(3);
				m_pSprite->m_End = 3;
			}
			//m_position.y += 5;
			if(marioState==Esmall)
				m_velocity.y = v;
			else
				m_velocity.y = v+2;
			hasJumped = true;
		}
	}
}

void Mario::Sit()
{
	marioAction = ESitting;
	int indextmp;
	if(marioState==Esmall || (previousState==Esmall&&(marioState==Esuperstar || marioState==Eimmortal)))
		indextmp = 3;
	else if(marioState==Ebig||marioState==Egun ||((previousState==Ebig || previousState ==Egun)&&(marioState==Esuperstar || marioState==Eimmortal)))
		indextmp = 4;
	else
		indextmp = 4;
	m_pSprite->m_Start = indextmp;
	m_pSprite->SetIndex(indextmp);
	m_pSprite->m_End = indextmp;
}

void Mario::Fall()
{
	hasJumped = true;
}

void Mario::Stop()
{
	if(hasJumped==false && marioAction!=EShooting)
	{
		if(m_velocity.x>0)
			m_velocity.x-= FRICTION;
		if(m_velocity.x <0)
			m_velocity.x+= FRICTION;
		if((m_velocity.x < 0.2f && m_velocity.x > 0)|| (m_velocity.x > -0.2f && m_velocity.x < 0))
			m_velocity.x = 0;
		m_pSprite->SetIndex(0);
		marioAction = EStanding;
	}
	else
	{
		if(m_velocity.x>0)
			m_velocity.x-=0.04f;
		if(m_velocity.x <0)
			m_velocity.x+=0.04f;	
	}
}

void Mario::Shoot()
{
	//Neu trang thai hien tai ko phai la Egun thi ko dc ban
	if(marioState!=Egun)
		return;
	if(marioAction!=EShooting && canShoot==true)
	{
		marioAction = EShooting;
		SoundManager::GetInst()->PlaySoundEffect(ESoundEffect::ES_Shoot);
		canShoot = false;
		Bullet *tmp = new Bullet(m_position + D3DXVECTOR2(0,25));
		if(m_effect == None)
			tmp->SetVelocity(D3DXVECTOR2(7,5) + m_velocity);
		else
			tmp->SetVelocity(D3DXVECTOR2(-7,5) + m_velocity);
		_creatingAfterObjects.push_back(tmp);
	}
}

void Mario::Update(float dt)
{
	m_pSprite->Update(dt);
	m_position.x+=m_velocity.x*dt*60;

	isInPiPe = false;

	//Cu 5 s luu lai vi tri hien tai 1 lan
	if(m_state!=Die) //neu dang o trang thai nay thi ko tinh thoi gian
	{	timeCheckLastPoint+=dt;
		if((int)timeCheckLastPoint % 5 == 0 && (int)timeCheckLastPoint > 5)
		{
			positionLastCheck = m_position;
			timeCheckLastPoint = 0;
		}
	}
	//Xu ly luc mario chet
	//Neu chet do roi xuong vuc
	if(m_position.y < -20 && m_state!=Die)
	{
		life--;
		marioState = Edie;
		hasJumped = false;
		SoundManager::GetInst()->StopAllBGSound();
		SoundManager::GetInst()->PlaySoundEffect(ES_MarioDie);
	}
	//Xu ly
	if(marioState==Edie)
	{
		timeChangeState+=dt;
		if(timeChangeState > 0.5f)
		{
			m_state = EState::Die;
			timeChangeState = 0;
		}
		m_pSprite->SetIndex(4);
		m_velocity.x = 0;
		Jump(20);				
	}
	//Hoi Sinh
	if(m_position.y < -9000)
		marioState = EResborn;
	if(marioState==EResborn && life>0)
	{
		SoundManager::GetInst()->PlayBGSound(EBGSound::EMainGameSound1);
		m_position = positionLastCheck;
		m_velocity = D3DXVECTOR2(0,0);
		marioState = Esmall;
		m_pSprite = smallMario;
		m_state = Alive;
		isImmortal = true;
	}
	//Update trang thai mario

	if(nextMarioState!=Enone)
	{
		ChangeState(dt);
	}
	//Xu ly thao tac ban dan cua mario
	if(marioAction==EShooting)
	{
		m_pSprite->SetIndex(1);
		timeShoot+=dt;
		if(timeShoot>=0.2f)
		{
			marioAction = EStanding;
			timeShoot=0;
		}
	}
	if(canShoot==false)
	{
		timeNextShoot+=dt;
		if(timeNextShoot>0.3f)
		{
			canShoot = true;
			timeNextShoot = 0;
		}
	}

	//Trang thai bat tu
	if(isImmortal==true)
	{
		timeImmortal+= dt;
		if(alpha_immortal>100 && tmpCheck==false)
		{
			alpha_immortal-=5;
			if(alpha_immortal<=100)
				tmpCheck = true;
		}
		if(alpha_immortal < 255 && tmpCheck==true)
		{
			alpha_immortal+=5;
			if(alpha_immortal>=255)
				tmpCheck=false;
		}

		if(timeImmortal > TIME_IMMORTAL_AFTER_HIT)
		{
			isImmortal = false;
			timeImmortal=0;
		}
	}
	else
	{
		alpha_immortal=255;
		tmpCheck = false;
	}
	//Trang thai Super star
	if(marioState==Esuperstar)
	{
		timeSuperStar+= dt;
		if(alpha_superstar>100 && tmpCheck2==false)
		{
			alpha_superstar-=10;
			if(alpha_superstar<=100)
				tmpCheck2 = true;
		}
		if(alpha_superstar < 255 && tmpCheck2==true)
		{
			alpha_superstar+=10;
			if(alpha_superstar>=255)
				tmpCheck2=false;
		}

		if(timeSuperStar > TIME_SUPER_STAR)
		{
			marioState = previousState;
			timeSuperStar=0;
		}
	}
	else
	{
		alpha_superstar = 255;
		tmpCheck2 = false;
	}
	//Xu ly hanh dong mario nhay
	if(hasJumped == true)
	{
		if(marioState ==Esmall)
		{
			m_pSprite->m_Start = 5;
			m_pSprite->SetIndex(5);
			m_pSprite->m_End = 5;
		}
		else if(marioState == Ebig || marioState==Egun)
		{
			m_pSprite->m_Start = 3;
			m_pSprite->SetIndex(3);
			m_pSprite->m_End = 3;
		}
		m_position.y+=m_velocity.y*dt*60;
		m_velocity.y -= 1.20f*dt*60;
	}
	if(hasJumped==false && isStandInBar == false)
	{
		m_velocity.y = 0;
	}
	
}

void Mario::UpdateCollision(list<GameObject*> listObjects, float dt)
{
	
	if(marioState==Edie)
		return;
	float timeTmp = 0;
	list<GameObject*>::iterator it;
	float moveX,moveY;	
	float normalx,normaly;
	float timeCols = 0;
	Box tmpBox = this->GetBox();
 	Box tmpBox2 = tmpBox;
 	tmpBox2.y -= 2;
	bool tmpIsFall = true;
	bool isTopCollision = false;
	float tmpTimeTopCollision = 0;
	for(it=listObjects.begin(); it!=listObjects.end(); ++it)
	{
		GameObject* otherObject = (*it);
		//neu doi tuong da chet thi bo qua
		if(otherObject->GetObjectState()==EState::Die || otherObject->GetObjectState()==EState::Breaking ||
			otherObject->GetObjectState()==EState::NoCheckCollision||
			otherObject->GetObjectState() == EState::KickUp)
			continue;
		EObjectType typeOther=otherObject->GetObjectType();
		Box otherBox = otherObject->GetBox();
		
		//////////////////////////////////////////////////////////////////////////
		//Kiem tra neu hien tai mario dang di tren Land
		if(AABB(tmpBox2,otherObject->GetBox(),moveX,moveY) == true)
		{
			if(typeOther == EBreakBrick || typeOther==EQuestionBox)
			{
				if(moveY !=0)
				{
					tmpIsFall = false;
					isStandInBar = false;
					isInPiPe = false;
				}
			}

			if( typeOther==EPipe )
			{
				Pipe* tmpPipe = (Pipe*)otherObject;
				if(moveY!=0)
				{
					if(tmpPipe->GetDirectToChange()==Colls_Bot)
						isInPiPe=true;	
					tmpIsFall = false;
				}
			}

			if(typeOther == ELand)
			{
				Land* tmpLand = (Land*)otherObject;
				if(tmpLand->GetVelocity().y == 0)
				{
					if(moveY !=0)
					{
						tmpIsFall = false;
						isStandInBar = false;
					}
				}
				else
				{
					isStandInBar = true;
					tmpIsFall = false;
				}
			}
		}

		


		if(AABB(tmpBox,otherBox,moveX,moveY) == false)
		{
			//thay doi van toc 2 box
			if(otherObject->GetKindOfObject()==EDynamic)
			{
				tmpBox.vx -= otherBox.vx;
				tmpBox.vy -= otherBox.vy;
				otherBox.vx = 0;
				otherBox.vy = 0;
			}
			else
			{
				tmpBox = this->GetBox();
			}

			if(AABBCheck(GetSweptBroadphaseBox(tmpBox),otherBox)==true || AABBCheck(GetSweptBroadphaseBox(otherBox),tmpBox)==true)
			{
	
				timeCols = SweptAABB(tmpBox,otherBox,normalx,normaly);

				if(timeCols > 0 && timeCols < 1)
				{
					timeCols*=dt;
					ECollisionDirect collDriect = GetCollisionDirect(normalx,normaly);
					float remainingtime = dt - timeCols;

					
					switch(typeOther)
					{
					case ELand:						
						{		
							switch(collDriect)
							{
							case Colls_Bot:
								{
									if(otherObject->GetVelocity().y < 0)
									{
										m_position.y += m_velocity.y*timeCols*60;
										m_velocity.y = otherObject->GetVelocity().y;									
										hasJumped = false;
										isStandInBar = true;
										tmpIsFall = false;
									}
									else
									{
										m_position.y += m_velocity.y*timeCols*60 + 1;
										hasJumped = false;
										tmpIsFall = false;
									}
								}
								break;
							case Colls_Top:
								{
									m_position.y += m_velocity.y*timeCols*60;
									m_velocity.y = -m_velocity.y/2;
								}
								break;
							case Colls_Left:
								{
 									m_position.x += m_velocity.x*timeCols*60 + 1;
									m_velocity.x = 0;
								}
								break;
							case Colls_Right:
								{
 									m_position.x += m_velocity.x*timeCols*60 - 1;
									m_velocity.x = 0;
								}
								break;
							}
						}
						break;
					//////////////////////////////---QuestionBox---//////////////////////////////////

					case EQuestionBox:
						{
							switch(collDriect)
							{
							case Colls_Bot:
								{
									m_position.y += m_velocity.y*timeCols*60 + 1;
									hasJumped = false;
									tmpIsFall = false;
								}
								break;
							case Colls_Top:
								{
									isTopCollision = true;
									tmpTimeTopCollision = timeCols;
									if(otherObject->GetObjectState()!=EState::Blocked)
									{
										otherObject->SetObjectState(Hit);
										QuestionBox* tmpQues = (QuestionBox*)otherObject;
										if(tmpQues->GetItem()->GetObjectType()==ECoin)
											gold++;
									}
								}
								break;
							case Colls_Left:
								{
									m_position.x += m_velocity.x*timeCols*60 + 1;
									m_velocity.x = 0;
								}
								break;
							case Colls_Right:
								{
									m_position.x += m_velocity.x*timeCols*60 - 1;
									m_velocity.x = 0;
								}
								break;
							}
						}
						break;
					//////////////////////////////---BreakBrick---//////////////////////////////////

					case  EBreakBrick:
						{		
							if(otherObject->GetObjectState()!=EState::Breaking)
							{
							
								switch(collDriect)
								{
								case Colls_Bot:
									{
										m_position.y += m_velocity.y*timeCols*60 + 1;
										hasJumped = false;
										tmpIsFall = false;
									}
									break;
								case Colls_Top:
									{
										isTopCollision = true;
										tmpTimeTopCollision = timeCols;
										if(marioState==Ebig || marioState==Egun || marioState==Esuperstar)
										{	
											otherObject->SetObjectState(EState::Hit);
											SoundManager::GetInst()->PlaySoundEffect(ES_BreakBrick);
										}
										if(marioState==Esmall)
											otherObject->SetObjectState(EState::Move);
									}
									break;
								case Colls_Left:
									{
										m_position.x += m_velocity.x*timeCols*60 + 1;
										m_velocity.x = 0;
									}
									break;
								case Colls_Right:
									{
										m_position.x += m_velocity.x*timeCols*60 - 1;
										m_velocity.x = 0;
									}
									break;
								}
							}
						}
						break;
					////////////////////////////////Pipie/////////////////////////////////////
					case  EPipe:
						{
							Pipe* tmpPipe = (Pipe*)otherObject;
							
							switch(collDriect)
							{
							case Colls_Bot:
								{
									m_position.y += m_velocity.y*timeCols*60 + 1;
									hasJumped = false;
									tmpIsFall = false;
								}
								break;
							case Colls_Top:
								{

								}
								break;
							case Colls_Left:
								{
									m_position.x += m_velocity.x*timeCols*60 + 1;
									m_velocity.x = 0;
								}
								break;
							case Colls_Right:
								{
									if(tmpPipe->GetDirectToChange()!=ECollisionDirect::Colls_Right)
									{
										m_position.x += m_velocity.x*timeCols*60 - 1;
										m_velocity.x = 0;
									}
								}
								break;
							}
						}
						break;
					////////////////////////////////Fungi///////////////////////////////////////
					case EFungi:
						{
							if(otherObject->GetObjectState()==EState::Hit)
								continue;
							Fungi* tmpFungi = (Fungi*)otherObject;
							if(marioState==Esuperstar)
							{
								SoundManager::GetInst()->PlaySoundEffect(ES_KickUp);
								otherObject->SetObjectState(KickUp);
								score+=100;
							}
							else
 							switch(collDriect)
							{
							case Colls_Bot:
								{
									m_position.y += m_velocity.y*timeCols*60 + 1;
									hasJumped = false;
									Jump(20);
									tmpIsFall = false;														
									tmpFungi->SetObjectState(EState::Hit);
									SoundManager::GetInst()->PlaySoundEffect(ES_FungiDie);
									score+=100;
								}
								break;
							case Colls_Top:
								{
									tmpFungi->SetFall(false);
									m_position.y += m_velocity.y*timeCols*60 - 1;
									m_velocity.y = -m_velocity.y;
									if(isImmortal==false)
										nextMarioState = Ehit;
								}
								break;
							case Colls_Left:
								{
									m_position.x += m_velocity.x*timeCols*60 + 1;
									m_velocity.x = -m_velocity.x;
									tmpFungi->SetDirection(EDirection::Left);
									if(isImmortal==false)
										nextMarioState = Ehit;
								}
								break;
							case Colls_Right:
								{
									m_position.x += m_velocity.x*timeCols*60 - 1;
									m_velocity.x = -m_velocity.x;
									Fungi* tmpFungi = (Fungi*)otherObject;
									tmpFungi->SetDirection(EDirection::Right);
									if(isImmortal==false)
										nextMarioState = Ehit;
								}
								break;
							}
						}
						break;
					////////////////////////////////Turtle///////////////////////////////////////
					case  ETurtle:
						{
							Turtle* tmpTurtle = (Turtle*)otherObject;
							if(marioState==Esuperstar)
							{
								SoundManager::GetInst()->PlaySoundEffect(ES_KickUp);
								otherObject->SetObjectState(KickUp);
								score+=100;
							}
							else
							switch(collDriect)
							{
							case Colls_Bot:
								{
									m_position.y += m_velocity.y*timeCols*60 + 1;
									hasJumped = false;
									Jump(15);
									tmpIsFall = false;						
									if(tmpTurtle->GetObjectState()==EState::Alive)
										tmpTurtle->SetObjectState(Hit);
									SoundManager::GetInst()->PlaySoundEffect(ES_TurtleHit);
									tmpTurtle->SetVelocity(D3DXVECTOR2(0,0));
									tmpTurtle->SetDirection(EDirection::NONE);
								}
								break;
							case Colls_Top:
								{
									tmpTurtle->SetFall(false);
									m_position.y += m_velocity.y*timeCols*60 - 1;
									m_velocity.y = -m_velocity.y;
									if(isImmortal==false)
									{
										hasJumped = false;
										Jump(20);
										nextMarioState = Ehit;
									}
								}
								break;
							case Colls_Left:
								{
									m_position.x += m_velocity.x*timeCols*60 + 5;
									m_velocity.x = -m_velocity.x;							
								
									if(tmpTurtle->GetVelocity().x !=0)
									{
										if(isImmortal==false)
										{
											hasJumped = false;
											Jump(20);
											nextMarioState = Ehit;
										}
									}		
									tmpTurtle->SetDirection(EDirection::Left);
									SoundManager::GetInst()->PlaySoundEffect(ES_TurtleHit);
								}
								break;
							case Colls_Right:
								{
									m_position.x += m_velocity.x*timeCols*60 - 5;
									m_velocity.x = -m_velocity.x;						
								
									if(tmpTurtle->GetVelocity().x !=0)
									{
										if(isImmortal==false)
										{
											hasJumped = false;
											Jump(20);
  											nextMarioState = Ehit;
										}
									}
									tmpTurtle->SetDirection(EDirection::Right);
									SoundManager::GetInst()->PlaySoundEffect(ES_TurtleHit);
								}
								break;
							}
						}
						break;
					///////////////////////////FlowerMonster//////////////////////////////
					case EFlowerMonster:
						{
							FlowerMonster* tmpMonster = (FlowerMonster*)otherObject;
							if(marioState==Esuperstar)
							{
								SoundManager::GetInst()->PlaySoundEffect(ES_KickUp);
								otherObject->SetObjectState(Die);
								score+=100;
							}
							else
							if(collDriect == Colls_Bot && m_velocity.y == 0)
							{
								tmpMonster->SetCanUp(false);
							}
							else
							{
								if(isImmortal==false)
								{
									hasJumped = false;
									Jump(20);
									nextMarioState = Ehit;
								}
							}
						}
						break;
					////////////////////////////MushRoom-Red//////////////////////////////
					//if (typeOther==EObjectType::EMushroom_Red)
					//{						
					//	nextMarioState = Ebig;
					//	otherObject->SetObjectState(EState::Die);
					//}
					//////////////////////////Flower//////////////////////////////////
					case  EFlower:
						{
							nextMarioState = Egun;
							otherObject->SetObjectState(EState::Die);
							SoundManager::GetInst()->PlaySoundEffect(ES_MarioGrowUp);
							score+=100;
						}
						break;
					///////////////////Coin///////////////////
					case ECoin:
						{						
							otherObject->SetObjectState(EState::Die);
							SoundManager::GetInst()->PlaySoundEffect(ES_Coin);
							gold++;
						}
						break;
					//////////////////Star///////////////////
					case EStar:
						{
							otherObject->SetObjectState(Die);
							nextMarioState = Esuperstar;
							SoundManager::GetInst()->PlaySoundEffect(ES_MarioGrowUp);
							score+=100;
						}
						break;
					///////////////////////Goal/////////////////////
					case EGoal:
						{
							otherObject->SetVelocity(D3DXVECTOR2(3,3));
							isLevelFinish = true;
						}
						break;
					}
				}
			}
		}
		else 
		{
			if(typeOther == ELand || typeOther == EBreakBrick || typeOther==EQuestionBox)
			{
				if(moveX<0)
				{
					m_velocity.x = - m_velocity.x;
					m_position.x += moveX - 1;
				}
				else if( moveX > 0)
				{
					m_velocity.x = - m_velocity.x;
					m_position.x += moveX + 1;
				}
				if(moveY!=0)
				{
					m_velocity.y = 0;
					m_position.y += moveY;
				}
			}
			if(typeOther==EPipe)
			{
				Pipe* tmpPipe = (Pipe*)otherObject;
				if(moveX<0 && tmpPipe->GetDirectToChange()!=Colls_Right)
				{
					m_velocity.x = - m_velocity.x;
					m_position.x += moveX - 1;
				}
				else if( moveX > 0)
				{
					m_velocity.x = - m_velocity.x;
					m_position.x += moveX + 1;
				}
				if(moveY!=0)
				{
					m_velocity.y = 0;
					m_position.y += moveY;
					hasJumped = false;
				}
			}

			if(typeOther == ETurtle)
			{
				Turtle* tmpTurtle = (Turtle*)otherObject;
				if(moveX < 0)		//Va cham phai
				{					
					m_position.x += moveX - 10;	
					if(tmpTurtle->GetVelocity().x !=0 && isImmortal==false)
						nextMarioState = Ehit;
					tmpTurtle->SetDirection(EDirection::Right);
					SoundManager::GetInst()->PlaySoundEffect(ES_TurtleHit);
				}
				else if( moveX > 0)	//Va cham trai
				{
					m_position.x += moveX + 10;
					if(tmpTurtle->GetVelocity().x !=0 && isImmortal==false)
						nextMarioState = Ehit;
					tmpTurtle->SetDirection(EDirection::Left);
					SoundManager::GetInst()->PlaySoundEffect(ES_TurtleHit);
				}
				if(moveY>0)			//Va cham duoi
				{
					hasJumped = false;
					Jump(20);				
					tmpTurtle->SetObjectState(Hit);
					tmpTurtle->SetVelocity(D3DXVECTOR2(0,0));
					tmpTurtle->SetDirection(EDirection::NONE);
					SoundManager::GetInst()->PlaySoundEffect(ES_TurtleHit);
				}
				else if(moveY<0)	//Va cham tren
				{
					tmpTurtle->SetFall(false);
					m_position.y += moveY - 1;
					m_velocity.y = -m_velocity.y/2;
					if(isImmortal==false)
						nextMarioState = Ehit;
				}
			}

			if(typeOther == EFungi)
			{
				if(otherObject->GetObjectState()==EState::Hit)
					continue;
				Fungi* tmpFungi = (Fungi*)otherObject;
				if(moveX < 0)
				{
					m_position.x += moveX - 1;
					tmpFungi->SetDirection(EDirection::Right);
					if(isImmortal==false)
						nextMarioState = Ehit;
				}
				else if( moveX > 0)
				{				
					m_position.x += moveX + 1;
					tmpFungi->SetDirection(EDirection::Left);
					if(isImmortal==false)
						nextMarioState = Ehit;
				}
				if(moveY>0)
				{
					hasJumped = false;
					Jump(20);				
					tmpFungi->SetObjectState(EState::Hit);
					SoundManager::GetInst()->PlaySoundEffect(ES_FungiDie);
					score+=100;
				}
				else if(moveY < 0)
				{
					tmpFungi->SetFall(false);
					m_position.y += moveY - 1;
					m_velocity.y = -m_velocity.y/2;
					if(isImmortal==false)
						nextMarioState = Ehit;
				}
			}

			////////////////////////////FlowerMonster//////////////////////
			if(typeOther == EFlowerMonster)
			{
				FlowerMonster* tmpMonster = (FlowerMonster*)otherObject;
				if(moveY!=0 && m_velocity.y == 0)
				{
					tmpMonster->SetCanUp(false);
				}
				else
				{
					if(moveY!=0)
					{
						hasJumped = false;
						Jump(20);
					}
					if(isImmortal==false)
						nextMarioState = Ehit;
				}
			}
			////////////////////////////Coin///////////////////////////////
			if(typeOther == EObjectType::ECoin)
			{
				gold++;
				otherObject->SetObjectState(EState::Die);
				SoundManager::GetInst()->PlaySoundEffect(ES_Coin);
			}
			////////////////////////////MushRoom-Red//////////////////////////////
			if (typeOther==EObjectType::EMushroom_Red)
			{						
				nextMarioState = Ebig;
				otherObject->SetObjectState(EState::Die);
				SoundManager::GetInst()->PlaySoundEffect(ES_MarioGrowUp);
				score+=100;
			}
			////////////////////////////MushRoom-Green//////////////////////////////
			if (typeOther==EObjectType::EMushroom_Green)
			{						
				life++;
				otherObject->SetObjectState(EState::Die);
				SoundManager::GetInst()->PlaySoundEffect(ES_MarioLifeUp);
				score+=100;
			}
			//////////////////////////Flower//////////////////////////////////
			if(typeOther == EFlower)
			{
				nextMarioState = Egun;
				otherObject->SetObjectState(EState::Die);
				SoundManager::GetInst()->PlaySoundEffect(ES_MarioGrowUp);
				score+=100;
			}
			//////////////////////////Goal////////////////////////////////
			if(typeOther == EGoal)
			{
				otherObject->SetVelocity(D3DXVECTOR2(4,4));
				isLevelFinish=true;
			}
		}
	}
	//Xu ly hanh dong nhay len va cham nhieu doi tuong 1 luc
	if(isTopCollision==true)
	{
		m_position.y += m_velocity.y*tmpTimeTopCollision*60;
		m_velocity.y = -m_velocity.y/4;
	}
	if(tmpIsFall==true)
		Fall();
}

void Mario::Draw()
{
	D3DCOLOR color = D3DCOLOR_ARGB(255,255,255,255);
	if(isImmortal==true)
		color = D3DCOLOR_ARGB(alpha_immortal,255,255,255);
	if(marioState==Esuperstar)
		color = D3DCOLOR_ARGB(alpha_superstar,alpha_superstar,0,0);
	m_pSprite->Draw(m_position,m_effect,0,1,0.5f,color);
}

void Mario::ChangeState(float dt)
{
	if(nextMarioState==Ebig)
	{
		if(marioState==Esmall)
		{
			m_pSprite = bigMario;
			m_position.y += 20;
			marioState = Ebig;
			nextMarioState = Enone;
			return;
		}
		if(marioState==Esuperstar && previousState==Esmall)
		{
			m_pSprite = bigMario;
			m_position.y += 20;
			previousState = Ebig;
			nextMarioState = Enone;
			return;
		}	
	}
	if(nextMarioState == Egun)
	{
		if(marioState==Esuperstar && previousState == Ebig)
		{
			m_pSprite = cowboyMario;
			previousState = Egun;
			nextMarioState = Enone;
			return;
		}
		if(marioState==Esuperstar && previousState == Esmall)
		{
			m_pSprite = bigMario;
			previousState = Ebig;
			nextMarioState = Enone;
			return;
		}
		if(marioState==Ebig)
		{
			m_pSprite = cowboyMario;
			marioState = Egun;
			nextMarioState = Enone;
			return;
		}
		if(marioState == Esmall)
		{
			m_pSprite = bigMario;
			marioState =  Ebig;
			nextMarioState = Enone;
			return;
		}
	}

	if(nextMarioState == Ehit)
	{
		if(marioState == Esmall)
		{
			life--;
			marioState = Edie;
			m_pSprite = smallMario;
			nextMarioState = Enone;
			SoundManager::GetInst()->StopAllBGSound();
			SoundManager::GetInst()->PlaySoundEffect(ES_MarioDie);
			return;
		}
		if(marioState == Ebig)
		{
			isImmortal = true;
			m_pSprite = smallMario;
			marioState = Esmall;
			nextMarioState = Enone;
			return;
		}
		if(marioState == Egun)
		{
			isImmortal = true;
			m_pSprite = bigMario;
			marioState = Ebig;
			nextMarioState = Enone;
			return;
		}
	}

	if(nextMarioState==Esuperstar)
	{
		previousState = marioState;
		marioState = nextMarioState;
		nextMarioState=Enone;
		return;
	}
}